//
//  PMenuController.swift
//  CJPack
//
//  Created by 陈警卫 on 2023-06-01.
//

import UIKit

class PMenuController: CJBaseBackController {

    override func viewDidLoad() {
        super.viewDidLoad()
        
        title = "多层布局"
        setupView()
        setupData()
    }
    
    deinit {
        NotificationCenter.default.removeObserver(self)
    }
    
    var tableView: CJMultGestTableView!
    var canScroll: Bool = true
    
    func setupView() {
        
        tableView = CJMultGestTableView(frame: CGRect(x: 0, y: CJScreen.navBatBarHeight,
                                              width: view.cj_width,
                                              height: view.cj_height - CJScreen.navBatBarHeight),
                                style: .plain)
        tableView.delegate = self
        tableView.dataSource = self
        view.addSubview(tableView)
        tableView.contentInsetAdjustmentBehavior = .never
        tableView.separatorStyle = .none
        tableView.showsVerticalScrollIndicator = false
        tableView.showsHorizontalScrollIndicator = false
        
        tableView.register(PMenuCell.self, forCellReuseIdentifier: "PMenuCell")
        tableView.register(PMenuContentCell.self, forCellReuseIdentifier: "PMenuContentCell")
    }
    
    func setupData() {
        NotificationCenter.default.addObserver(self, selector: #selector(scrollMenuSubTop(noti: )), name: .init("cj.menu.sub.top"), object: nil)
    }
    
    @objc func scrollMenuSubTop(noti: NSNotification) {
        canScroll = true
    }
}

extension PMenuController: UITableViewDelegate, UITableViewDataSource {
    
    func scrollViewDidScroll(_ scrollView: UIScrollView) {
        
        let bottomOffset = self.tableView.contentSize.height - self.tableView.cj_height
        if bottomOffset > 0 {
            if scrollView.contentOffset.y >= bottomOffset {
                let offset = scrollView.contentOffset.y - bottomOffset
                scrollView.contentOffset = CGPoint(x: 0, y: bottomOffset)
                NotificationCenter.default.post(name: .init("cj.menu.main.bottom"), object: offset)
                if canScroll {
                    canScroll = false
                }
            } else {
                if !canScroll {
                    scrollView.contentOffset = CGPoint(x: 0, y: bottomOffset)
                }
            }
        }
    }
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 5
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        if indexPath.row == 4 {
            let cell = tableView.dequeueReusableCell(withIdentifier: "PMenuContentCell", for: indexPath)
            cell.selectionStyle = .none
            return cell
        } else {
            let cell = tableView.dequeueReusableCell(withIdentifier: "PMenuCell", for: indexPath)
            cell.selectionStyle = .none
            if let cell = cell as? PMenuCell {
                cell.titleL.text = String(indexPath.row)
            }
            return cell
        }
    }
    
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        if indexPath.row == 4 {
            return tableView.cj_height
        } else {
            return 100
        }
    }
}
